package com.zzyq.pc.web.configure.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Splitter;
import com.zzyq.pc.common.StandardDataFormat;
import com.zzyq.pc.common.base.BaseController;
import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.utils.CommonUtils;
import com.zzyq.pc.utils.EncodingUtils;
import com.zzyq.pc.utils.HttpUtils;
import com.zzyq.pc.utils.LoggerUtils;
import com.zzyq.pc.web.configure.model.Enshrine;
import com.zzyq.pc.web.configure.service.IEnshrineService;

@Controller
@RequestMapping(value="/enshrine/",produces="application/json;charset=UTF-8")
public class EnshrineController extends BaseController{
	
	@Autowired
	IEnshrineService enshrineService;
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 添加收藏
	 * @param enshrine
	 * @param releaseTime1
	 */
	@ResponseBody
	@RequestMapping("insert")
	public String insert(HttpServletRequest request,HttpServletResponse response,Enshrine enshrine,String releaseTime1){
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR,MSG_ERROR, null);
		try {
			//判断对象不能为空
			if (CommonUtils.isObjectNull(enshrine)) {
				return HttpUtils.formatData(dateFormat,CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
			}
			//字符转换
			if (StringUtils.isNotBlank(enshrine.getEnshrineAuthor())) {
				enshrine.setEnshrineAuthor(EncodingUtils.fixUrlParamEncoding(request, enshrine.getEnshrineAuthor()));
			}
			if (StringUtils.isNotBlank(enshrine.getEnshrineTitle())) {
				enshrine.setEnshrineTitle(EncodingUtils.fixUrlParamEncoding(request, enshrine.getEnshrineTitle()));
			}
			if (enshrine.getEnshrineTitle().length()>200) {
				String strtitle = enshrine.getEnshrineTitle().substring(0, 200);
				enshrine.setEnshrineTitle(strtitle);
			}
			Date releaseTime = null;
			if (StringUtils.isNotBlank(releaseTime1)) {
				releaseTime = format.parse(releaseTime1);
				enshrine.setReleaseTime(releaseTime);
			}
			if (enshrineService.insert(enshrine)) {
				return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, null);
			} else {
				return HttpUtils.formatData(dateFormat, CODE_ERROR, MSG_ERROR, null);
			}
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "添加收藏错误", e.getMessage());
			e.printStackTrace();
			System.out.println(e);
            return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	/**
	 * 收藏列表
	 * @param pageIndex
	 * @param pageSize
	 * @param title
	 */
	@ResponseBody
	@RequestMapping("selectAll")
	public String selectAll(HttpServletRequest request,HttpServletResponse resopnse,Integer pageIndex,Integer pageSize
			,String title){
		List<Enshrine> list = new ArrayList<Enshrine>();
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
		try {
			if (null == pageIndex||pageIndex <= 0 ) {
				pageIndex = 1;
			}
			if (null == pageSize || pageSize <= 0 ) {
				pageSize = 15;
			}
			PageView pageView = new PageView(pageSize, pageIndex);
			if (StringUtils.isNotBlank(title)) {
				//字符转换
				title = EncodingUtils.fixUrlParamEncoding(request, title.trim());
			}
			list = enshrineService.selectAll(pageView, title);
			
			dateFormat.setTotalPages(pageView.getPageCount());
			dateFormat.setTotalRecords(pageView.getRowCount());
            return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, list);
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "查询错误", e.getMessage());
			e.printStackTrace();
			System.out.println(e);
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, list);
		}
	}
	/**
	 * 取消收藏
	 * @param ifmId
	 * @param articleId
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest request,HttpServletResponse response,String ifmId,String articleId){
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
		try {
			if (StringUtils.isBlank(ifmId)||StringUtils.isBlank(articleId)) {
				return HttpUtils.formatData(dateFormat, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
			}
			if (enshrineService.deleteById(ifmId, articleId)) {
				return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, null);
			} else {
				return HttpUtils.formatData(dateFormat, CODE_ERROR, MSG_ERROR, null);
			}
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "取消收藏错误", e.getMessage());
			e.printStackTrace();
			System.out.println(e);
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
	/**
	 * 批量取消收藏
	 * @param request
	 * @param response
	 * @param ids
	 * @return
	 */
	@RequestMapping("deleteAll")
	@ResponseBody
	public String deleteAll(HttpServletRequest request,HttpServletResponse response,String ids){
		StandardDataFormat dateFormat = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
		List<String> list = new ArrayList<String>();
		boolean b = false;
		try {
			if (StringUtils.isNotBlank(ids)) {
				list = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(ids);
			}
			for (int i = 0; i < list.size(); i++) {
				b = enshrineService.deleteByPrimaryKey(Integer.valueOf(list.get(i)));
			}
			if (b==true) {
				return HttpUtils.formatData(dateFormat, CODE_OK, MSG_OK, null);
			} else {
				return HttpUtils.formatData(dateFormat, CODE_ERROR, MSG_ERROR, null);
			}
		} catch (Exception e) {
			LoggerUtils.fmtError(getClass(), "取消收藏错误", e.getMessage());
			e.printStackTrace();
			System.out.println(e);
			return HttpUtils.formatData(dateFormat, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
}
